package cn.tomoki.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import cn.tomoki.config.URL;
import cn.tomoki.config.UserId;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

/**
 * @author 仓友树
 * 预约记录详情页
 */
public class AppointHistoryDetailsActivity extends AppCompatActivity {
    private TextView appointName;
    private TextView appointNumber;
    private TextView appointTime;
    private TextView innerWidthView;
    private TextView outerWidthView;
    private TextView tbodyView;
    private TextView holeView;
    private TextView pbodyView;
    private TextView weightView;
    private TextView noAppoint;
    private TextView yesAppoint;
    private ImageButton myBreak;
    private RelativeLayout relativeLayout;
    private int mark;
//    private static String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appoint_history_details);
        appointName = findViewById(R.id.appointName);
        appointNumber = findViewById(R.id.appointNumber);
        appointTime = findViewById(R.id.appointTime);
        innerWidthView = findViewById(R.id.personalInnerWidth);
        outerWidthView = findViewById(R.id.personalOuterWidth);
        tbodyView = findViewById(R.id.personalTbody);
        holeView = findViewById(R.id.personalHole);
        pbodyView = findViewById(R.id.personalPbody);
        weightView = findViewById(R.id.personalWeight);
        noAppoint = findViewById(R.id.noAppoint);
        yesAppoint = findViewById(R.id.yesAppoint);
        myBreak = findViewById(R.id.breakButton);
        relativeLayout = findViewById(R.id.textData);
//        userId = UserId.UserId;



        //接收上一个activity传来的mark
        Bundle bundle = this.getIntent().getExtras();
        mark = bundle.getInt("mark");
//        int mark = Integer.parseInt(markString);
        interfaceModile();
        setListeners();
    }

    private void setListeners() {
        OnClick onClick = new OnClick();
        myBreak.setOnClickListener(onClick);
        noAppoint.setOnClickListener(onClick);
        yesAppoint.setOnClickListener(onClick);
    }

    private class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Integer integer = null;
            switch (v.getId()){
                case R.id.breakButton:
                    finish();
                    break;
                case R.id.noAppoint:
                    send("0");
                    finish();
                    break;
                case R.id.yesAppoint:
                    send("2");
                    finish();
                    break;
            }
        }
    }
    private void interfaceModile() {
//        int mark = Integer.parseInt(markString);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
//                    FormBody.Builder params = new FormBody.Builder();
//                    params.add("mark",mark);
                    String url = URL.url+"/app/getAppointByMark?mark="+mark;
                    //创造HTTP客户端
                    OkHttpClient client = new OkHttpClient()
                            .newBuilder()
                            .connectTimeout(200, TimeUnit.SECONDS)
                            .readTimeout(250, TimeUnit.SECONDS)
                            .build();
                    //创造http请求
                    Request request = new Request.Builder()
                            .url(url)
                            .build();
                    //执行发送的指令
                    Response response = client.newCall(request).execute();
                    //获取后端返回的结果
                    String responseDate = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseDate);
                    String name = jsonObject.getString("productName");
                    String number = jsonObject.getString("number");
                    String time = jsonObject.getString("time");
                    String innerWidth = jsonObject.getString("innerWidth");
                    String outerWidth = jsonObject.getString("outerWidth");
                    String tbody = jsonObject.getString("tbody");
                    String hole = jsonObject.getString("hole");
                    String pbody = jsonObject.getString("pbody");
                    String weight = jsonObject.getString("weight");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            appointName.setText(name);
                            appointNumber.setText(number);
                            appointTime.setText(time);
                            innerWidthView.setText(innerWidth);
                            outerWidthView.setText(outerWidth);
                            tbodyView.setText(tbody);
                            holeView.setText(hole);
                            pbodyView.setText(pbody);
                            weightView.setText(weight);
                            Toast.makeText(AppointHistoryDetailsActivity.this,"连接成功啦~",Toast.LENGTH_LONG).show();
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AppointHistoryDetailsActivity.this,"连接不上网络啦",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    }

    private void send(String state){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = URL.url+"/app/changeState?mark="+mark+"&state="+state;
                    OkHttpClient client = new OkHttpClient()
                            .newBuilder()
                            .connectTimeout(200, TimeUnit.SECONDS)
                            .readTimeout(250, TimeUnit.SECONDS)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .build();
                    Response response = client.newCall(request).execute();
                    boolean responseDate = Boolean.parseBoolean(response.body().string());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AppointHistoryDetailsActivity.this,"成功啦"+responseDate, Toast.LENGTH_SHORT);
                            finish();
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AppointHistoryDetailsActivity.this, "出现异常了~~~(>_<)~~~", Toast.LENGTH_SHORT);
                        }
                    });
                }
            }
        }).start();
    }
}